// 修复资源数据中的capabilities字段，统一为标准格式
const fs = require('fs');
const path = require('path');

// 标准能力配置模板
const standardCapabilities = {
  FORM: { enabled: true, form_type: 'resource_form' },
  TABLE: { enabled: true, table_type: 'resource_table' },
  WORKFLOW: { enabled: true, workflow_type: 'resource_workflow' },
  RULE: { enabled: true, rule_type: 'resource_rule' },
  API: { enabled: true, api_type: 'resource_api' },
  AUTH: { enabled: true, auth_type: 'resource_auth' },
  EVENT: { enabled: true, event_type: 'resource_events' },
  DELEGATE: { enabled: true, delegate_type: 'resource_delegate' },
};

// 读取资源数据文件
const resourcesFile = path.join(
  __dirname,
  '../apps/backend-mock/api/resource-center/resources.get.ts',
);
let content = fs.readFileSync(resourcesFile, 'utf8');

// 查找所有capabilities字段并修复
const capabilitiesRegex = /capabilities: JSON\.stringify\(\{([^}]+)\}\)/g;

content = content.replace(capabilitiesRegex, (match, innerContent) => {
  // 检查是否已经包含标准能力
  if (innerContent.includes('FORM: { enabled: true')) {
    return match; // 已经是标准格式，不需要修改
  }

  // 保留原有的自定义能力，添加标准能力
  const customCapabilities = innerContent.trim();
  const newCapabilities = `\n        ${Object.entries(standardCapabilities)
    .map(
      ([key, value]) =>
        `${key}: { enabled: true, ${key.toLowerCase()}_type: '${value[`${key.toLowerCase()}_type`]}' }`,
    )
    .join(
      ',\n        ',
    )},${customCapabilities ? `\n        ${customCapabilities}` : ''}`;

  return `capabilities: JSON.stringify({${newCapabilities}\n      })`;
});

// 写回文件
fs.writeFileSync(resourcesFile, content, 'utf8');

console.log('✅ 资源数据中的capabilities字段已修复完成！');
console.log('📝 所有资源现在都包含标准的能力配置格式');
